/*
* type              请求的方式  默认为get
* url               发送请求的地址
* param             发送请求的参数
* isShowLoader      是否显示loader动画  默认为false
* dataType          返回JSON数据  默认为JSON格式数据
* callBack          请求的回调函数
*/
(function(){

    function AjaxRequest(opts){
        this.type         = opts.type || "get";
        this.url          = opts.url;
        this.param        = opts.param || {};
        this.isShowLoader = opts.isShowLoader || false;
        this.loaderIn     = opts.loaderIn || "body";

        this.loaderStyle  = opts.loaderStyle || 1;  // 1 方框模式 2 点模式

        this.loaderTime   = opts.loaderTime || 100;

        this.dataType     = opts.dataType || "json";

        this.async        = opts.async || true;

        this.callBack     = opts.callBack;
        this.init();  
    }

    AjaxRequest.prototype = {

        //初始化
        init: function(){
            this.sendRequest();
        },
        //渲染loader
        showLoader: function(){

            if(this.isShowLoader){

                var tag = this.loaderIn.replace(".","").replace("#","")

                if (this.loaderStyle === 1) {
                    var loader = '<div class="'+tag+'-box loader-box">'
                    +   '<div class="square-box-loader">'
                    +       '<div class="square-box-loader-container">'
                    +           '<div class="square-box-loader-corner-top"></div>'
                    +           '<div class="square-box-loader-corner-bottom"></div>'
                    +       '</div>'
                    +       '<div class="square-box-loader-square"></div>'
                    +    '</div>'
                    +'</div>';
                }

                if (this.loaderStyle === 2) {
                    var loader = '<div class="'+tag+'-box loader-box">'
                    +  '<div class="dot-opacity-loader">'
                    +     '<span></span>'
                    +     '<span></span>'
                    +     '<span></span>'
                    +  '</div>'
                    +'</div>';
                }


                $(this.loaderIn).append(loader);

            }

        },
        //隐藏loader
        hideLoader: function(){

            var tag = this.loaderIn.replace(".","").replace("#","")

            var loader = "." + tag + "-box"
            if(this.isShowLoader) $(loader).remove();
        },
        //发送请求
        sendRequest: function(){
            var self = this;

            try {


                $.ajax({

                    type: this.type,
                    url: this.url,
                    data: this.param,
                    dataType: this.dataType,
                    beforeSend: this.showLoader(),
                    cache: false,
                    async: this.async,
                    error: function (XMLHttpRequest) {
                        if (parseInt(XMLHttpRequest.status) !== 200) {

                            var p = {
                                code: 0,
                                info: 'E' + XMLHttpRequest.status + ' - 服务异常，请稍候刷新页面再试！'
                            }
                            $.toastMsg.showToast(p, 3000, function(){})

                        } else {
                            console.log("AJAX ERROR ==200", XMLHttpRequest.responseText);
                        }
                    },

                    success: function(res){

                        setTimeout(function(){

                            if (res.code === 0) { // 业务报错提示
                                $.toastMsg.showToast(res, 6000, function(){})
                            }

                            self.hideLoader();

                            if(self.callBack){
                                if (Object.prototype.toString.call(self.callBack) === "[object Function]") {   //Object.prototype.toString.call方法--精确判断对象的类型
                                    self.callBack(res);
                                }else{
                                    console.log("callBack is not a function");
                                }
                            }

                        }, self.loaderTime)


                    }
                });

            } catch (e) {

                console.log(e)

            }
        }
    };

    window.AjaxRequest = AjaxRequest;
})();